package org.example.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.example.domain.po.ErpMaterialAttribute;
import org.example.domain.po.ErpUnit;
import org.example.domain.po.ErpUser;
import org.example.domain.po.R;
import org.example.service.ErpMaterialAttributeService;
import org.example.service.impl.ErpMaterialAttributeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/commodity/material_attribute")
@Api("/多属性")
public class ErpMaterialAttributeController {
    @Autowired
    private ErpMaterialAttributeServiceImpl erpMaterialAttributeServiceimpl;
    @Autowired
    private  ErpMaterialAttributeService attributeService;
    //分页查询
    @GetMapping("/findByPage")
    public R<Page<ErpMaterialAttribute>> findByPage (
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            String attributeName,
            @RequestAttribute("user") ErpUser user
    ){
        Page<ErpMaterialAttribute> page = new Page<>(current, size);
        try {
            page = erpMaterialAttributeServiceimpl.findByPage(page, attributeName,user.getTenantId() );
            return R.success(page);
        } catch (Exception e) {
            return R.fail("查询失败：" + e.getMessage());
        }
    }

    @GetMapping("/list")
    public R<List<ErpMaterialAttribute>> selectByExample(@RequestAttribute("user") ErpUser user){
        List<ErpMaterialAttribute> list = attributeService.selectByExample(user.getTenantId());
        return R.success(list);
    }


    //添加
    @PostMapping("/insert")
    public R insert(@RequestBody ErpMaterialAttribute ErpMaterialAttribute, @RequestAttribute("user") ErpUser user){
        ErpMaterialAttribute.setTenantId(user.getTenantId());
        ErpMaterialAttribute.setDeleteFlag("0");
        erpMaterialAttributeServiceimpl.save(ErpMaterialAttribute);
        return R.success();
    }
    //删除
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") Integer id){
        erpMaterialAttributeServiceimpl.removeById(id);
        return R.success();
    }
    //    批量删除
    @DeleteMapping("/delete")
    public R deleteBatch(@RequestBody List<Long> ids) {
        for (Long id : ids) {
            erpMaterialAttributeServiceimpl.removeById(id);
        }
        return R.success();
    }
    //修改
    @PostMapping("/update")
    public R update(@RequestBody  ErpMaterialAttribute test){
        erpMaterialAttributeServiceimpl.updateById(test);
        return R.success();
    }
}